




//
//  NFourIconView.m
//  nncloud
//
//  Created by macfai on 2018/9/18.
//  Copyright © 2018年 youedata. All rights reserved.
//

#import "NFourIconView.h"
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "NFourIconViewCell.h"

@interface NFourIconView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView *iconCollectionView;

@property(nonatomic,strong)NSArray *imageViewArray;

@end

static NSString *iconCellID = @"icon";


@implementation NFourIconView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        //        [self makeUI];
        self.backgroundColor = kMagentaColor;
        
    }
    
    return self;
    
}


-(void)setIconArray:(NSArray *)iconArray{
    
    _iconArray = iconArray;
    
    //数据接收完成后再开始布局
    
    [self makeUIWithNumber:iconArray.count];
    
    _iconCollectionView.dataSource = self;
    
    
    
    
}

-(void)makeUIWithNumber:(NSInteger)num{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置itemSize
    ////这个属性决定了可以急等分屏幕并决定 是否滚动
    layout.itemSize = CGSizeMake(ScreenWidth/num,70.f);
    
    ///横向的  ******** 这个是关键中的关键   如果要横向布局，这个是必须的
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //这三个属性至关重要
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(8, 0, 0, 0);
    
    //2.初始化collectionView
    _iconCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth,self.frame.size.height) collectionViewLayout:layout];
    _iconCollectionView.bounces = YES;
    _iconCollectionView.delegate = self;
    _iconCollectionView.showsHorizontalScrollIndicator = false;
    _iconCollectionView.backgroundColor = kWhiteColor;
    [_iconCollectionView registerClass:[NFourIconViewCell class] forCellWithReuseIdentifier:iconCellID];

    [self addSubview:_iconCollectionView];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _iconArray.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NFourIconViewCell *cell = (NFourIconViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:iconCellID forIndexPath:indexPath];
    
    cell.iconImage.image = WDImgName(self.iconArray[indexPath.row]);
    cell.nameLabel.text = self.titleArray[indexPath.row];
    
    //    if (_iconArray.count > indexPath.row) {
    //        WDFourIconModel *model = _iconArray[indexPath.row];
    //        [cell configData:model];
    //    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NFourIconModel *model = self.iconArray[indexPath.row];
    if (_indexBlock) {
        
        NSLog(@"%ld",(long)indexPath.row);
        
        _indexBlock(indexPath.row,model);
        
    }
    
}


@end
